package com.financial.business.base.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.financial.business.base.connector.BalanceBusinessFeign;
import com.financial.business.base.connector.HeemoneyBusinessFeign;
import com.financial.business.base.connector.IntegralBusinessFeign;
import com.financial.business.base.connector.YsBusinessFeign;
import com.financial.business.base.constants.FinancialBaseContstants;
import com.financial.business.base.constants.FinancialBusinessTypeConstants;
import com.financial.business.base.service.FinancialBaseService;
import com.financial.business.base.utils.JsonUtils;
import com.financial.business.base.utils.apiresult.ApiResult;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author WANGNING
 * @api {post}
 * @email wyn20140226@aliyun.com
 * @tar 用于检测该服务是否就绪
 * @date 2019/11/4 15:37
 * @apiGroup base-business
 */
@Service
public class FinancialBaseServiceImpl implements FinancialBaseService {

    public static final Logger logger = LoggerFactory.getLogger(FinancialBaseServiceImpl.class);

    @Autowired
    private YsBusinessFeign ysBusinessFeign;

    @Autowired
    private HeemoneyBusinessFeign heemoneyBusinessFeign;

    @Autowired
    private BalanceBusinessFeign balanceBusinessFeign;

    @Autowired
    private IntegralBusinessFeign integralBusinessFeign;

    @Override
    public String report(HttpServletRequest request) {
        String requestJson = JsonUtils.analysisJson(request);
        logger.info("FinancialBaseServiceImpl.report()------报备获取http传过来的数据为:" + requestJson);
        /*将请求的json数据转换成map进行业务判断*/
        Map<String, Object> businessMap = JsonUtils.jsonToMap(requestJson);
        String businessType = (String) businessMap.get("business_type");
        if (StringUtils.isNotBlank(businessType)) {
            JSONObject object = (JSONObject) businessMap.get("business_msg");
            String businessMsg = object.toJSONString();
            logger.info("FinancialBaseServiceImpl.report()------获取到的支付业务类型为:" + businessType);
            ApiResult resultJson = ApiResult.failure(FinancialBaseContstants.ENUM_BUSINESSTYPE_EXCE);
            /*银盛报备*/
            if (businessType.equals(FinancialBusinessTypeConstants.HEEMONEY_REPORT.valueStr())) {
                resultJson = ysBusinessFeign.ysReport(businessMsg);
            } else if (businessType.equals(FinancialBusinessTypeConstants.HEEMONEY_PAY.valueStr())) {
                /*汇收银报备*/
                resultJson = heemoneyBusinessFeign.report(businessMsg);
            }
            /*根据业务类型请求对应的业务*/
            return JsonUtils.objectToJson(resultJson);
        }
        return JsonUtils.objectToJson(ApiResult.failure(FinancialBaseContstants.ENUM_BUSINESSTYPE_EXCE));
    }

    @Override
    public String pay(HttpServletRequest request) {
        String requestJson = JsonUtils.analysisJson(request);
        logger.info("FinancialBaseServiceImpl.pay()------支付获取http传过来的数据为:" + requestJson);
        /*将请求的json数据转换成map进行业务判断*/
        Map<String, Object> businessMap = JsonUtils.jsonToMap(requestJson);
        String businessType = (String) businessMap.get("business_type");
        if (StringUtils.isNotBlank(businessType)) {
            JSONObject object = (JSONObject) businessMap.get("business_msg");
            String businessMsg = object.toJSONString();
            logger.info("FinancialBaseServiceImpl.pay()------获取到的支付业务类型为:" + businessType);
            ApiResult resultJson = ApiResult.failure(FinancialBaseContstants.ENUM_BUSINESSTYPE_EXCE);
            /*银盛支付*/
            if (businessType.equals(FinancialBusinessTypeConstants.YS_PAY.valueStr())) {
                resultJson = ysBusinessFeign.ysPay(businessMsg);
            } else if (businessType.equals(FinancialBusinessTypeConstants.HEEMONEY_PAY.valueStr())) {
                /*汇收银支付*/
                resultJson = heemoneyBusinessFeign.pay(businessMsg);
            } else if (businessType.equals(FinancialBusinessTypeConstants.BALANCE_PAY.valueStr())) {
                /*余额支付*/
                resultJson = balanceBusinessFeign.pay(businessMsg);
            } else if (businessType.equals(FinancialBusinessTypeConstants.INTEGRAL_PAY.valueStr())) {
                /*积分支付*/
                resultJson = integralBusinessFeign.pay(businessMsg);
            }
            /*根据业务类型请求对应的业务*/
            return JsonUtils.objectToJson(resultJson);
        }
        return JsonUtils.objectToJson(ApiResult.failure(FinancialBaseContstants.ENUM_BUSINESSTYPE_EXCE));
    }

    @Override
    public String split(HttpServletRequest request) {
        String requestJson = JsonUtils.analysisJson(request);
        logger.info("FinancialBaseServiceImpl.split()------分账获取http传过来的数据为:" + requestJson);
        /*将请求的json数据转换成map进行业务判断*/
        Map<String, Object> businessMap = JsonUtils.jsonToMap(requestJson);
        String businessType = (String) businessMap.get("business_type");
        if (StringUtils.isNotBlank(businessType)) {
            JSONObject object = (JSONObject) businessMap.get("business_msg");
            String businessMsg = object.toJSONString();
            logger.info("FinancialBaseServiceImpl.split()------获取到的支付业务类型为:" + businessType);
            ApiResult resultJson = ApiResult.failure(FinancialBaseContstants.ENUM_BUSINESSTYPE_EXCE);
            /*银盛分账*/
            if (businessType.equals(FinancialBusinessTypeConstants.YS_SPLIT.valueStr())) {
                resultJson = ysBusinessFeign.ysSplit(businessMsg);
            } else if (businessType.equals(FinancialBusinessTypeConstants.HEEMONEY_SPLIT.valueStr())) {
                /*汇收银分账*/
                resultJson = heemoneyBusinessFeign.split(businessMsg);
            }
            /*根据业务类型请求对应的业务*/
            return JsonUtils.objectToJson(resultJson);
        }
        return JsonUtils.objectToJson(ApiResult.failure(FinancialBaseContstants.ENUM_BUSINESSTYPE_EXCE));
    }

    @Override
    public String refund(HttpServletRequest request) {
        String requestJson = JsonUtils.analysisJson(request);
        logger.info("FinancialBaseServiceImpl.refund()------退款获取http传过来的数据为:" + requestJson);
        /*将请求的json数据转换成map进行业务判断*/
        Map<String, Object> businessMap = JsonUtils.jsonToMap(requestJson);
        String businessType = (String) businessMap.get("business_type");
        if (StringUtils.isNotBlank(businessType)) {
            JSONObject object = (JSONObject) businessMap.get("business_msg");
            String businessMsg = object.toJSONString();
            logger.info("FinancialBaseServiceImpl.refund()------获取到的支付业务类型为:" + businessType);
            ApiResult resultJson = ApiResult.failure(FinancialBaseContstants.ENUM_BUSINESSTYPE_EXCE);
            /*银盛退款*/
            if (businessType.equals(FinancialBusinessTypeConstants.YS_REFUND.valueStr())) {
                resultJson = ysBusinessFeign.refund(businessMsg);
            } else if (businessType.equals(FinancialBusinessTypeConstants.HEEMONEY_REFUND.valueStr())) {
                /*汇收银退款*/
                resultJson = heemoneyBusinessFeign.refund(businessMsg);
            } else if (businessType.equals(FinancialBusinessTypeConstants.BALANCE_REFUND.valueStr())) {
                /*余额支付*/
                resultJson = balanceBusinessFeign.refund(businessMsg);
            } else if (businessType.equals(FinancialBusinessTypeConstants.INTEGRAL_REFUND.valueStr())) {
                /*积分支付*/
                resultJson = integralBusinessFeign.refund(businessMsg);
            }
            /*根据业务类型请求对应的业务*/
            return JsonUtils.objectToJson(resultJson);
        }
        return JsonUtils.objectToJson(ApiResult.failure(FinancialBaseContstants.ENUM_BUSINESSTYPE_EXCE));
    }

    @Override
    public String withdrawal(HttpServletRequest request) {
        String requestJson = JsonUtils.analysisJson(request);
        logger.info("FinancialBaseServiceImpl.withdrawal()------提现获取http传过来的数据为:" + requestJson);
        /*将请求的json数据转换成map进行业务判断*/
        Map<String, Object> businessMap = JsonUtils.jsonToMap(requestJson);
        String businessType = (String) businessMap.get("business_type");
        if (StringUtils.isNotBlank(businessType)) {
            JSONObject object = (JSONObject) businessMap.get("business_msg");
            String businessMsg = object.toJSONString();
            logger.info("FinancialBaseServiceImpl.withdrawal()------获取到的支付业务类型为:" + businessType);
            ApiResult resultJson = ApiResult.failure(FinancialBaseContstants.ENUM_BUSINESSTYPE_EXCE);
            /*银盛提现*/
            if (businessType.equals(FinancialBusinessTypeConstants.YS_WITHDRAWAL.valueStr())) {
                resultJson = ysBusinessFeign.withdrawal(businessMsg);
            } else if (businessType.equals(FinancialBusinessTypeConstants.HEEMONEY_WITHDRAWAL.valueStr())) {
                /*汇收银提现*/
                resultJson = heemoneyBusinessFeign.withdrawal(businessMsg);
            }
            /*根据业务类型请求对应的业务*/
            return JsonUtils.objectToJson(resultJson);
        }
        return JsonUtils.objectToJson(ApiResult.failure(FinancialBaseContstants.ENUM_BUSINESSTYPE_EXCE));
    }

    @Override
    public String payment(HttpServletRequest request) {
        String requestJson = JsonUtils.analysisJson(request);
        logger.info("FinancialBaseServiceImpl.payment()------代付获取http传过来的数据为:" + requestJson);
        /*将请求的json数据转换成map进行业务判断*/
        Map<String, Object> businessMap = JsonUtils.jsonToMap(requestJson);
        String businessType = (String) businessMap.get("business_type");
        if (StringUtils.isNotBlank(businessType)) {
            JSONObject object = (JSONObject) businessMap.get("business_msg");
            String businessMsg = object.toJSONString();
            logger.info("FinancialBaseServiceImpl.payment()------获取到的支付业务类型为:" + businessType);
            ApiResult resultJson = ApiResult.failure(FinancialBaseContstants.ENUM_BUSINESSTYPE_EXCE);
            /*银盛代付、转账*/
            if (businessType.equals(FinancialBusinessTypeConstants.YS_PAYMENT.valueStr())) {
                resultJson = ysBusinessFeign.payment(businessMsg);
            } else if (businessType.equals(FinancialBusinessTypeConstants.HEEMONEY_PAYMENT.valueStr())) {
                /*汇收银代付、转账*/
                resultJson = heemoneyBusinessFeign.payment(businessMsg);
            } else if (businessType.equals(FinancialBusinessTypeConstants.BALANCE_PAYMENT.valueStr())) {
                /*余额支付*/
                resultJson = balanceBusinessFeign.payment(businessMsg);
            } else if (businessType.equals(FinancialBusinessTypeConstants.INTEGRAL_PAYMENT.valueStr())) {
                /*积分支付*/
                resultJson = integralBusinessFeign.payment(businessMsg);
            }
            /*根据业务类型请求对应的业务*/
            return JsonUtils.objectToJson(resultJson);
        }
        return JsonUtils.objectToJson(ApiResult.failure(FinancialBaseContstants.ENUM_BUSINESSTYPE_EXCE));
    }

    @Override
    public String query(HttpServletRequest request) {
        String requestJson = JsonUtils.analysisJson(request);
        logger.info("FinancialBaseServiceImpl.query()------获取查询http传过来的数据为:" + requestJson);
        /*将请求的json数据转换成map进行业务判断*/
        Map<String, Object> businessMap = JsonUtils.jsonToMap(requestJson);
        String businessType = (String) businessMap.get("business_type");
        if (StringUtils.isNotBlank(businessType)) {
            JSONObject object = (JSONObject) businessMap.get("business_msg");
            String businessMsg = object.toJSONString();
            logger.info("FinancialBaseServiceImpl.query()------获取到的支付业务类型为:" + businessType);
            ApiResult resultJson = ApiResult.failure(FinancialBaseContstants.ENUM_BUSINESSTYPE_EXCE);
            /*银盛查询*/
            if (businessType.equals(FinancialBusinessTypeConstants.YS_QUERY.valueStr())) {
                resultJson = ysBusinessFeign.query(businessMsg);
            } else if (businessType.equals(FinancialBusinessTypeConstants.HEEMONEY_QUERY.valueStr())) {
                /*汇收银查询*/
                resultJson = heemoneyBusinessFeign.query(businessMsg);
            } else if (businessType.equals(FinancialBusinessTypeConstants.BALANCE_QUERY.valueStr())) {
                /*余额查询*/
                resultJson = balanceBusinessFeign.query(businessMsg);
            } else if (businessType.equals(FinancialBusinessTypeConstants.INTEGRAL_QUERY.valueStr())) {
                /*积分查询*/
                resultJson = integralBusinessFeign.query(businessMsg);
            }
            /*根据业务类型请求对应的业务*/
            return JsonUtils.objectToJson(resultJson);
        }
        return JsonUtils.objectToJson(ApiResult.failure(FinancialBaseContstants.ENUM_BUSINESSTYPE_EXCE));
    }
}
